<?php
class FloorAction extends LoginAction
{
    /**
     * 商场楼层显示
     * @author Ice iceinto@mallog.com.cn
     */
    public function market ()
    {
        $id = $this->_get('id', 'int');
        $this->setMarketData($id);
        $FloorModel = M('Floor');
        $map = array();
        $map['mid'] = $id;
        $floors = $FloorModel->where($map)->select();
        $this->assign('floors', $floors);
        $this->display();
    }
    /**
     * 添加楼层操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function add ()
    {
        $id = $this->_get('id');
        $this->setMarketData($id);
        if (isPost()) {
            $FloorModel = D('Floor');
            $data = $this->getData();
            $status = $FloorModel->floorAdd($data);
            if ($status['status'] == 1) {
                $this->successMessage('添加成功！');
            } else {
                $this->errorMessage($status['doc']);
            }
        }
        $this->display();
    }
    /**
     * 修改楼层操作
     * @author Ice iceinto@mallog.com.cn
     */
    public function edit ()
    {
        $mid = (int) $this->_get('market');
        $id = (int) $this->_get('id');
        $this->setFloorData($id);
        $this->setMarketData($mid);
        if ($this->isPost()) {
            $FloorModel = D('Floor');
            $data = $this->getData();
            $data['id'] = $id;
            $status = $FloorModel->floorUpdate($data);
            if ($status['status'] == 1) {
                $this->setFloorData($id);
                $this->successMessage('修改成功！');
            } else {
                $this->errorMessage($status['doc']);
            }
        }
        $this->display();
    }
    /**
     * 设置和判断商场参数
     * @author Ice iceinto@mallog.com.cn
     */
    public function setMarketData ($id)
    {
        $MarketModel = M('market');
        $map = array();
        $map['id'] = $id;
        $market = $MarketModel->where($map)->field('id,name')->find();
        if ($market) {
            $this->assign('market', $market);
        } else {
            $this->error('数据传递有错误！');
        }
    }
    /**
     * 设置和判断商场参数
     * @author Ice iceinto@mallog.com.cn
     */
    public function setFloorData ($id)
    {
        $FloorModel = M('Floor');
        $map = array();
        $map['id'] = $id;
        $floor = $FloorModel->where($map)->find();
        if ($floor) {
            $this->assign('floor', $floor);
        } else {
            $this->error('数据传递有错误！');
        }
    }
    /**
     * 获取提交数据
     * @author Ice iceinto@mallog.com.cn
     */
    public function getData ()
    {
        $data = array();
        $data['sain'] = strtoupper($this->_post('fSain'));
        $data['name'] = $this->_post('fName');
        $data['mid'] = $this->_post('mId');
        //判断必要性
        foreach ($data as $k => $d) {
            if (! $d) {
                $this->error($k . '不能为空！');
            }
        }
        return $data;
    }
    /**
     *  获取商场楼层信息
     *  @author Ice iceinto@mallog.com.cn
     */
    public function get ()
    {
        $FloorModel = M('Floor');
        $mid = $this->_get('mid');
        $map = array();
        if ($mid) {
            $map['mid'] = $mid;
            $data = $FloorModel->where($map)->field('id,mid,concat(sain,name) as name')->select();
            $this->ajaxReturn($data, null, 1);
        }
        $this->ajaxReturn(null, null, 0);
    }
}
?>